﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using Visiblox.Charts;

namespace myDiet
{
    public partial class HistoryPage : PhoneApplicationPage
    {
        App app = App.Current as App;
        public HistoryPage()
        {
            InitializeComponent();
        }

//============================================= radio button "day" checked !! ====================================================
        private void day_Checked(object sender, System.Windows.RoutedEventArgs e)
        { 
            //set client
            Random rand = new Random();
            WebClient dayClient = new WebClient();
            dayClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(dayClient_DownloadStringCompleted);
            dayClient.DownloadStringAsync(new Uri("http://" + app.url + "/MyDiet/cross1day.php?r=" + rand.Next(), UriKind.RelativeOrAbsolute));
        }

        void dayClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //if connection error, show error in MessageBox
                MessageBox.Show(e.Error.Message);
                return;
            }

            //set veriable for sum value
            double sumWeight = 0, sumCalorie = 0, sumProtein = 0, sumFat = 0, sumCarbo = 0;

            //set element variable
            XElement dayElement = XElement.Parse(e.Result);
            var items = from item in dayElement.Descendants("uid")          //start at element "uid"
                        select new NutritionData
                        {
                            //feed element value to new Nutrition class
                            WEight = item.Element("weight").Value,
                            CAlorie = item.Element("calorie").Value,
                            PRotien = item.Element("protein").Value,
                            FAt = item.Element("fat").Value,
                            CArbohydrate = item.Element("cabohydrate").Value
                        };

            //sum value of each variable
            foreach (var dayItems in items)
            {
                sumWeight += Convert.ToDouble(dayItems.WEight);
                sumCalorie += Convert.ToDouble(dayItems.CAlorie);
                sumProtein += Convert.ToDouble(dayItems.PRotien);
                sumFat += Convert.ToDouble(dayItems.FAt);
                sumCarbo += Convert.ToDouble(dayItems.CArbohydrate);
                
            }

            //show result to TextBox >> ReadOnly mode
            WeightTxtbox.Text = sumWeight.ToString();
            CalorieTxtbox.Text = sumCalorie.ToString();
            ProteinTxtbox.Text = sumProtein.ToString();
            FatTxtbox.Text = sumFat.ToString();
            CarboTxtbox.Text = sumCarbo.ToString();
        }
//============================================================================================================================

        
//============================================= radio button "week" checked !! ===============================================
        private void week_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            //set client
            Random rand = new Random();
            WebClient weekClient = new WebClient();
            weekClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(weekClient_DownloadStringCompleted);
            weekClient.DownloadStringAsync(new Uri("http://" + app.url + "/MyDiet/cross7day.php?r=" + rand.Next(), UriKind.RelativeOrAbsolute));
        }

        void weekClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //if connection error, show error in MessageBox
                MessageBox.Show(e.Error.Message);
                return;
            }

            //set veriable for sum value
            double sumWeight = 0, sumCalorie = 0, sumProtein = 0, sumFat = 0, sumCarbo = 0;

            //set element variable
            XElement dayElement = XElement.Parse(e.Result);
            var items = from item in dayElement.Descendants("uid")          //start at element "uid"
                        select new NutritionData
                        {
                            //feed element value to new Nutrition class
                            WEight = item.Element("weight").Value,
                            CAlorie = item.Element("calorie").Value,
                            PRotien = item.Element("protein").Value,
                            FAt = item.Element("fat").Value,
                            CArbohydrate = item.Element("cabohydrate").Value
                        };

            //sum value of each variable
            foreach (var dayItems in items)
            {
                sumWeight += Convert.ToDouble(dayItems.WEight);
                sumCalorie += Convert.ToDouble(dayItems.CAlorie);
                sumProtein += Convert.ToDouble(dayItems.PRotien);
                sumFat += Convert.ToDouble(dayItems.FAt);
                sumCarbo += Convert.ToDouble(dayItems.CArbohydrate);

            }

            //show result to TextBox >> ReadOnly mode
            WeightTxtbox.Text = sumWeight.ToString();
            CalorieTxtbox.Text = sumCalorie.ToString();
            ProteinTxtbox.Text = sumProtein.ToString();
            FatTxtbox.Text = sumFat.ToString();
            CarboTxtbox.Text = sumCarbo.ToString();
        }
//============================================================================================================================

        
        
//=========================================== radio button "month" checked !! ================================================
        private void month_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
        	//set client
            Random rand = new Random();
            WebClient monthClient = new WebClient();
            monthClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(monthClient_DownloadStringCompleted);
            monthClient.DownloadStringAsync(new Uri("http://" + app.url + "/MyDiet/cross30day.php?r=" + rand.Next(), UriKind.RelativeOrAbsolute));
        }

        void monthClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //if connection error, show error in MessageBox
                MessageBox.Show(e.Error.Message);
                return;
            }

            //set veriable for sum value
            double sumWeight = 0, sumCalorie = 0, sumProtein = 0, sumFat = 0, sumCarbo = 0;

            //set element variable
            XElement dayElement = XElement.Parse(e.Result);
            var items = from item in dayElement.Descendants("uid")          //start at element "uid"
                        select new NutritionData
                        {
                            //feed element value to new Nutrition class
                            WEight = item.Element("weight").Value,
                            CAlorie = item.Element("calorie").Value,
                            PRotien = item.Element("protein").Value,
                            FAt = item.Element("fat").Value,
                            CArbohydrate = item.Element("cabohydrate").Value
                        };

            //sum value of each variable
            foreach (var dayItems in items)
            {
                sumWeight += Convert.ToDouble(dayItems.WEight);
                sumCalorie += Convert.ToDouble(dayItems.CAlorie);
                sumProtein += Convert.ToDouble(dayItems.PRotien);
                sumFat += Convert.ToDouble(dayItems.FAt);
                sumCarbo += Convert.ToDouble(dayItems.CArbohydrate);

            }

            //show result to TextBox >> ReadOnly mode
            WeightTxtbox.Text = sumWeight.ToString();
            CalorieTxtbox.Text = sumCalorie.ToString();
            ProteinTxtbox.Text = sumProtein.ToString();
            FatTxtbox.Text = sumFat.ToString();
            CarboTxtbox.Text = sumCarbo.ToString();
        }
//========================================================================================================================
    }
}
